package com.example.stock.entity.dao;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.stock.comm.Cons;
import com.example.stock.comm.FreqEnum;
import com.example.stock.entity.eo.ProBarCbEo;
import com.example.stock.entity.mapper.ProBarCbMapper;
import com.example.stock.vo.DataVo;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProBarCbDao extends ServiceImpl<ProBarCbMapper, ProBarCbEo> {


    public List<DataVo> getRowDataWithFreq(String tsCode,List<String> tsCodes, LocalDateTime startTime, LocalDateTime endTime, FreqEnum freqEnum) {
        List<ProBarCbEo> list = list(
                Wrappers.<ProBarCbEo>lambdaQuery()
                        .eq(StrUtil.isNotBlank(tsCode), ProBarCbEo::getTsCode, tsCode)
                        .in(CollectionUtil.isNotEmpty(tsCodes),ProBarCbEo::getTsCode,tsCodes)
                        .gt(ProBarCbEo::getTradeTime,startTime)
                        .le(ProBarCbEo::getTradeTime,endTime)
                        .orderByAsc(ProBarCbEo::getTradeTime)
        );
        return list.stream()
                .filter(t -> freqEnum == FreqEnum.MIN1 ? true : freqEnum.listTime().contains(t.getTradeTime().format(Cons.formatHHmm)))
                .map(t -> new DataVo().setPrice(t.getClose()).setTradeTime(t.getTradeTime()).setTsCode(t.getTsCode()))
                .collect(Collectors.toList());
    }
}
